home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World Tours: Brazil
/
WorldTours: Brazil.iso
/
mac
/
content
/
main.dir
/
00120_Script_TextScrollBar v1.05
< prev
Wrap
Text File
|
2007-04-22
|
14KB
|
431 lines
--------------------------------------------------------------------------------------------------------------
-- Custom ScrollBar v1.05
-- DESCRIPTION
-- Apply this behaviour to a placeholder shape streched into the right height for the scrollbar.
-- This behaviour will create a temporay bitmap (using the bitmap references described below) if necessary.
-- The drag-handle resizes (and indicates how much text there is to scroll)
-- Specify the #text or #field member this widget controls. Note that the scrolling member is
-- expected to be a sprite on stage during the life of this behaviour's instance
-- UPDATES/CHANGES
-- 9/9/02:
--> Added some optimsation so that the image is only updated when the either the text changes or the scroll
-- state has changed (thanks Seb for the suggestion)
--> Creating/erasing members can now be handled by script "CastMemberFactory"
--> The scrollbar rect is now determined by the rect of the scrolling target, rather than the dummy sprite.
-- 8/9/02:
--> Added flag to GPDL configuration so that the checking for changing text can be switched off.
--------------------------------------------------------------------------------------------------------------
-- DEPENDENCIES
--------------------------------------------------------------------------------------------------------------
-- The behaviour expects to find the following bitmap cast members (these should
-- all be of the same width; "ScrollGuide" and "ScrollHandle_Middle" should be stretchable)
-- ï†"ScrollUpArrow"
-- ï†"ScrollUpArrow Pressed"
-- ï†"ScrollDnArrow"
-- ï†"ScrollDnArrow Pressed"
-- ï†"ScrollGuide"
-- ï†"ScrollHandle_BottomEnd"
-- ï†"ScrollHandle_Middle"
-- ï†"ScrollHandle_TopEnd"
-- If a new cast member is to be generated, then this behaviour will use script ("CastMemberFactory") - if available
--------------------------------------------------------------------------------------------------------------
-- CONFIGURATION
--------------------------------------------------------------------------------------------------------------
-- Specify the #text or #field member this widget controls. Note that the scrolling member is
-- expected to be a sprite on stage during the life of this behaviour's instance
property myTarget, myTargetSprite, myCheckForTextChangesFlag, myCreateMemberFlag
-- INTERNAL PROPERTIES
property mySprite, myOriginalMember, myMember, myEraseMemberFlag
property myElements, myImage, myCanvas, myOriginalRect
property myUpArrowRect, myDnArrowRect, myGuideRect, myHandleRect
property myMouseTrack, myMouseOffSet, myHandleFlag, myLastScrollPos
property myUpdateFlag, myLastTextCheck, myHandleDrawRect, myHandleOnFlag
--------------------------------------------------------------------------------------------------------------
-- PUBLIC
--------------------------------------------------------------------------------------------------------------
on beginSprite (me)
me.mInit()
end
--on endSprite (me)
-- me.mDestroy()
--end
on exitframe (me)
me.mHandleControls()
if myUpdateFlag then me.mUpdate()
end
on mousedown (me)
pointInSideControl = the mouseLoc - mySprite.loc
if inside(pointInSideControl, myHandleRect) then
-- drag the handle
myMouseTrack = #Drag
myMouseOffSet = the mouseV - myHandleRect.top
else if inside(pointInSideControl, myGuideRect) then
-- jump to point and then drag the handle
pnt = pointInSideControl.locV - myGuideRect.top
me.mJumpToPointV(pnt)
myMouseTrack = #Drag
myMouseOffSet = the mouseV - myHandleRect.top
else if inside(pointInSideControl, myUpArrowRect) then
-- scrollUp
myTarget.scrollByLine(-1)
myMouseTrack = #ScrollUp
myImage.copyPixels( myElements[#ScrollUpArrowPress], myUpArrowRect, myElements[#ScrollUpArrowPress].rect)
me.mUpdate()
else if inside(pointInSideControl, myDnArrowRect) then
-- scrollDn
myTarget.scrollByLine(1)
myMouseTrack = #Scrolldn
myImage.copyPixels( myElements[#ScrollDnArrowPress], myDnArrowRect, myElements[#ScrollDnArrowPress].rect)
me.mUpdate()
end if
end
on mouseUp (me)
myMouseTrack = 0
me.mRestoreButtons()
end
on mouseUpOutside (me)
myMouseTrack = 0
me.mRestoreButtons()
end
--------------------------------------------------------------------------------------------------------------
-- AUTHORING
--------------------------------------------------------------------------------------------------------------
on getpropertyDescriptionList (me)
pdList = [:]
pdList[#myTarget] = [:]
-- find text/fields
foundMembers = []
repeat with chan = 1 to the lastChannel
-- put sprite(chan).member.type
if sprite(chan).member.type = #text OR sprite(chan).member.type = #field then foundMembers.add(sprite(chan).member)
end repeat
if foundMembers.count = 0 then
put "Error finding a text or field sprite"
exit
else
pdList[#myTarget][#Comment] = "Text/Field to scroll (should be a sprite on the stage)"
pdList[#myTarget][#Format] = #member
pdList[#myTarget][#Range] = foundMembers
pdList[#myTarget][#Default] = 0
pdList[#myCheckForTextChangesFlag] = [:]
pdList[#myCheckForTextChangesFlag][#Comment] = "Check for changing text"
pdList[#myCheckForTextChangesFlag][#Format] = #boolean
pdList[#myCheckForTextChangesFlag][#Default] = 1
pdList[#myCreateMemberFlag] = [:]
pdList[#myCreateMemberFlag][#Comment] = "Create new member?"
pdList[#myCreateMemberFlag][#Format] = #boolean
pdList[#myCreateMemberFlag][#Default] = sprite(chan).member.type <> #bitmap
pdList[#myEraseMemberFlag] = [:]
pdList[#myEraseMemberFlag][#Comment] = "Erase this new member on endSprite?"
pdList[#myEraseMemberFlag][#Format] = #boolean
pdList[#myEraseMemberFlag][#Default] = member("CastMemberFactory").type <> #Script
return pdList
end if
end
on mInit (me)
mySprite = sprite(me.spriteNum)
if myCreateMemberFlag then
if member("CastMemberFactory").type = #Script then
idStr = "ScrollBar for " & myTarget.name & me.spriteNum
myMember = script("CastMemberFactory").mCreate(idStr, #bitmap)
else
myMember = new(#bitmap)
myMember.name = "TEMP"
end if
else
myMember = mySprite.member
end if
-- find the sprite with the target text/field
repeat with i = 1 to the lastChannel
if sprite(i).member = myTarget then
myTargetSprite = sprite(i)
exit repeat
end if
end repeat
if voidP(myTargetSprite) then
put "Error locating sprite contain text/field member " & myMember.name
sprite(me.spriteNum).scriptInstanceList.deleteOne(me)
end if
-- get the elements from reference bitmaps
myElements = [:]
myElements[#ScrollUpArrow] = member("ScrollUpArrow").image.duplicate()
myElements[#ScrollUpArrowPress] = member("ScrollUpArrow Pressed").image.duplicate()
myElements[#ScrollDnArrow] = member("ScrollDnArrow").image.duplicate()
myElements[#ScrollDnArrowPress] = member("ScrollDnArrow Pressed").image.duplicate()
myElements[#ScrollGuide] = member("ScrollGuide").image.duplicate()
-- myElements[#ScrollHandle_BottomEnd] = member("ScrollHandle_BottomEnd").image.duplicate()
myElements[#ScrollHandle_Middle] = member("ScrollHandle_Middle").image.duplicate()
-- myElements[#ScrollHandle_TopEnd] = member("ScrollHandle_TopEnd").image.duplicate()
-- construct the scrollbar
H = myTargetSprite.height
W = myElements[#ScrollUpArrow].width
myOriginalRect = mySprite.rect
myImage = image(W, H, 24)
myImage.fill(myImage.rect, rgb(232,232,232))
L = myTargetSprite.right
T = myTargetSprite.top
R = L + myImage.width
B = T + myImage.height
newRect = rect(L,T,R,B)
-- the arrows
myUpArrowRect = rect(0,0,W, myElements[#ScrollUpArrow].height)
myImage.copyPixels( myElements[#ScrollUpArrow], myUpArrowRect, myElements[#ScrollUpArrow].rect )
myDnArrowRect = rect(0,(H-myElements[#ScrollDnArrow].height),W,H)
myImage.copyPixels( myElements[#ScrollDnArrow], myDnArrowRect, myElements[#ScrollDnArrow].rect)
-- the guide
myGuideRect = rect(0,myElements[#ScrollUpArrow].height,W,(H-myElements[#ScrollDnArrow].height))
myImage.copyPixels( myElements[#ScrollGuide], myGuideRect, myElements[#ScrollGuide].rect)
-- the handle
myHandleRect = myGuideRect.duplicate()
myMember.image = myImage
myMember.regPoint = point(0,0)
myCanvas = myMember.image
myOriginalMember = mySprite.member
myOriginalRect = mySprite.rect
mySprite.member = myMember
mySprite.rect = newRect
myLastTextCheck = ""
me.mUpdate()
end
on mDestroy (me)
mySprite.member = myOriginalMember
mySprite.rect = myOriginalRect
if myCreateMemberFlag then
if myEraseMemberFlag then
if member("CastMemberFactory").type = #Script then member("CastMemberFactory").mErase(myMember)
else erase myMember
end if
end if
end
--------------------------------------------------------------------------------------------------------------
-- PRIVATE
--------------------------------------------------------------------------------------------------------------
on mRestoreButtons (me)
myImage.copyPixels( myElements[#ScrollUpArrow], myUpArrowRect, myElements[#ScrollUpArrow].rect)
myImage.copyPixels( myElements[#ScrollDnArrow], myDnArrowRect, myElements[#ScrollDnArrow].rect)
me.mUpdateImage()
end
on mJumpToPointV (me, aPntV)
scrollPos = MAX(myGuideRect.top, MIN((myGuideRect.bottom-myHandleRect.Height), aPntV))
myHandleRect = rect(myGuideRect.left, scrollPos, myGuideRect.right, scrollPos+ myHandleRect.Height)
if myGuideRect.height <> myHandleRect.Height then
portionToScroll = float(scrollPos - myGuideRect.top) / (myGuideRect.height - myHandleRect.Height)
maxScrolltop = Max (0, myTarget.height - myTargetSprite.height)
theScrollTop = (portionToScroll * maxScrolltop)
myTarget.scrollTop = integer(theScrollTop)
else
myTarget.scrollTop = 0
end if
end
--
on mHandleControls (me)
case( myMouseTrack ) of
#Drag:
scrollPos = MAX(myGuideRect.top, MIN((myGuideRect.bottom-myHandleRect.Height), the mouseV - myMouseOffSet))
if scrollPos = myLastScrollPos then
myUpdateFlag = 0
return
else
myLastScrollPos = scrollPos
end if
myHandleRect = rect(myGuideRect.left, scrollPos, myGuideRect.right, (scrollPos+ myHandleRect.Height))
-- update the text member
if myGuideRect.height <> myHandleRect.Height then
portionToScroll = float(scrollPos - myGuideRect.top) / (myGuideRect.height - myHandleRect.Height)
maxScrolltop = Max(0, myTarget.height -myTargetSprite.height)
theScrollTop = (portionToScroll * maxScrolltop)
myTarget.scrollTop = integer(theScrollTop)
else
myTarget.scrollTop = 0
end if
myUpdateFlag = 1
#ScrollUp:
myTarget.scrollByLine(-1)
myUpdateFlag = 1
#ScrollDn:
myTarget.scrollByLine(1)
myUpdateFlag = 1
otherwise
-- check if the text has changed
if myCheckForTextChangesFlag then
if myTarget.text <> myLastTextCheck then
myLastTextCheck = myTarget.text
myUpdateFlag = 1
return
end if
end if
myUpdateFlag = 0
end case
end
on mUpdate (me)
p = myTargetSprite.height -- pageheight
h = myTarget.height
portionVisible = float(p)/h
if portionVisible > 1 then
-- text smaller than page
myHandleRect = myGuideRect.duplicate()
myHandleHeight = myGuideRect.height
myHandleFlag = 0
if myHandleOnFlag then
myHandleOnFlag = 0
myImage.copyPixels( myElements[#ScrollGuide], myGuideRect, myElements[#ScrollGuide].rect)
end if
else
myHandleFlag = 1
-- Ensure a minimun height for the handle
myHandleHeight = MAX(20, myGuideRect.height * portionVisible)
--
theScrollTop = myTarget.scrollTop
maxScrolltop = h - p
if maxScrolltop > 0 then
portionScrolled = float(theScrollTop) / maxScrolltop
handlePos = integer( (myGuideRect.height-myHandleHeight) * portionScrolled )
T = myGuideRect.top + handlePos
B = T + myHandleHeight
R = rect(myGuideRect.left, T, myGuideRect.right, B)
if R = myHandleRect then myHandleFlag = 0
else myHandleRect = R
else
-- text smaller than page
myHandleRect = myGuideRect.duplicate()
myHandleHeight = myGuideRect.height
myHandleFlag = 0
if myHandleOnFlag then
myHandleOnFlag = 0
myImage.copyPixels( myElements[#ScrollGuide], myGuideRect, myElements[#ScrollGuide].rect)
end if
end if
end if
me.mUpdateImage()
updateStage
end
on mUpdateImage (me)
if myHandleFlag then -- add the handle (if required)
handle = me.mGetHandleImage()
myImage.copyPixels( myElements[#ScrollGuide], myGuideRect, myElements[#ScrollGuide].rect)
myImage.copyPixels( handle, myHandleRect, handle.rect)
myHandleOnFlag = 1
end if
myCanvas.copyPixels( myImage, myImage.rect, myImage.rect)
end
on mGetHandleImage (me)
handle = image(myHandleRect.width, myHandleRect.height, 24)
destRect = rect(0,0,myElements[#ScrollHandle_Middle].width, myHandleRect.height)
handle.copyPixels(myElements[#ScrollHandle_Middle], destRect, myElements[#ScrollHandle_Middle].rect)
return handle
end